//
//  ZPEvaluateCell.m
//  ZhenPin
//
//  Created by fei on 14-7-10.
//  Copyright (c) 2014年 zhenpin. All rights reserved.
//

#import "ZPEvaluateCell.h"
#import "UIImageView+WebCache.h"
#import "ZPShowEvaluateImageVC.h"
#define BEGIN_X 12
#define BEGIN_Y 5
#define Show_ImageView_Tag 1000
@implementation ZPEvaluateCell
{
    NSArray *imageArray;
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier WithSourceDict:(NSDictionary *)dict {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setSubViewsWith:dict];
    }
    return self;
}

- (id)initWithStyle:(UITableViewCellStyle)style justForHeightWithSourceDict:(NSDictionary *)dict {
    self = [super initWithStyle:style reuseIdentifier:nil];
    if (self) {
        [self setSubViewsWith:dict];
    }
    return self;
    
}


-(void)setSubViewsWith:(NSDictionary *)sourceDict {
    float height = 0;
    
    UILabel *userNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(BEGIN_X, BEGIN_Y, 190, 20)];
    userNameLabel.textAlignment = NSTextAlignmentLeft;
    userNameLabel.font = [UIFont systemFontOfSize:15];
    userNameLabel.textColor = RGB(51, 51, 51);
    userNameLabel.text = [NSString stringWithFormat:@"%@",sourceDict[@"userName"]];
    userNameLabel.backgroundColor = [UIColor clearColor];
    [userNameLabel sizeToFit];
    [self.contentView addSubview:userNameLabel];
    for (int i = 0; i < 5; i ++) {
        UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(userNameLabel.frame.size.width+10+BEGIN_X+16*i, BEGIN_Y+2, 12, 12)];
        [imageV setImage:[UIImage imageNamed:@"star_small_01"]];
        [self.contentView addSubview:imageV];
    }
    
    
    
    height = BEGIN_Y + 25;
    UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(BEGIN_X, height, 320-BEGIN_X*2, 300)];
    contentLabel.textAlignment = NSTextAlignmentLeft;
    contentLabel.font = [UIFont systemFontOfSize:14];
    contentLabel.textColor = [UIColor darkGrayColor];
    contentLabel.numberOfLines = 0;
    contentLabel.lineBreakMode = NSLineBreakByCharWrapping;
    contentLabel.text = [NSString stringWithFormat:@"%@",sourceDict[@"content"]];
    [contentLabel sizeToFit];
    
    contentLabel.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:contentLabel];
    
    
    
    height = height + contentLabel.frame.size.height+10;
    
    imageArray = sourceDict[@"showImagesArray"];
    if ([imageArray isEqual:[NSNull null]] || imageArray.count == 0) {
        
    } else {
        for (int i = 0 ; i < (imageArray.count>6?6:imageArray.count);  i ++) {
//            UIImageView *backImage = [[UIImageView alloc] initWithFrame:CGRectMake(BEGIN_X+49*i, height, 41, 41)];
//            backImage.image = [UIImage imageWithContentsOfFile:path];
//            [self.contentView addSubview:backImage];
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(BEGIN_X+50*i, height, 40, 40)];
            [imageView sd_setImageWithURL:Return_imageUrl([imageArray objectAtIndex:i], 80) placeholderImage:[UIImage imageNamed:@"evaluate_noimage"]];
            imageView.tag = Show_ImageView_Tag+i;
            imageView.userInteractionEnabled = YES;
            UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOnImage:)];
            [imageView addGestureRecognizer:tapGesture];
            [self.contentView addSubview:imageView];
        }
        height += 50;
    }
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSString *string_date = [NSString stringWithFormat:@"%@",sourceDict[@"time"]];
    if ([string_date isEqualToString:[NSString stringWithFormat:@"%@",[NSNull null]]] || string_date.length <= 0) {
        
    } else {
        NSString *dataString = [formatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:[sourceDict[@"time"] longLongValue]]];
        
        UILabel *dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(BEGIN_X, height, 200, 15)];
        dateLabel.textAlignment = NSTextAlignmentLeft;
        dateLabel.font = [UIFont systemFontOfSize:14];
        dateLabel.textColor = RGB(154, 154, 154);
        dateLabel.text = dataString;
        dateLabel.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:dateLabel];
        height = height + dateLabel.frame.size.height;
        height += 15;
    }
    
    
    
    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.height, height);
}

-(void)setCellHeightWithDict:(NSDictionary *)sourceDict {
    float height = 0;
    height = BEGIN_Y + 25;
    UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(BEGIN_X, height, 250, 300)];
    contentLabel.textAlignment = NSTextAlignmentLeft;
    contentLabel.font = [UIFont systemFontOfSize:14];
    contentLabel.textColor = [UIColor darkGrayColor];
    contentLabel.numberOfLines = 0;
    contentLabel.lineBreakMode = NSLineBreakByCharWrapping;
    contentLabel.text = [NSString stringWithFormat:@"%@",sourceDict[@"content"]];
    [contentLabel sizeToFit];
    height = height + contentLabel.frame.size.height+10;
    NSString *array = sourceDict[@"showImagesArray"];
    if (array) { //图片高度  40
        height += 50;
        
    }
    
    UILabel *dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(BEGIN_X, height, 200, 20)];
    dateLabel.textAlignment = NSTextAlignmentRight;
    dateLabel.font = [UIFont systemFontOfSize:14];
    dateLabel.textColor = [UIColor grayColor];
    dateLabel.text = [NSString stringWithFormat:@"%@",sourceDict[@"time"]];
    dateLabel.backgroundColor = [UIColor clearColor];
    
    height = height + dateLabel.frame.size.height;
    height += 15;
    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.height, height);
}

-(void)tapOnImage:(UITapGestureRecognizer *)tapGesture {
    
    UIImageView *imageView = (UIImageView *)[tapGesture view];
    if (imageArray.count > imageView.tag - Show_ImageView_Tag) {
        //获取点图图片url
        ZPShowEvaluateImageVC *showVC = [[ZPShowEvaluateImageVC alloc] init];
        showVC.imageArray = imageArray;
        showVC.selectIndex = imageView.tag - Show_ImageView_Tag;
        if ([_delegate_image respondsToSelector:@selector(evaluateCellDidSelectItemAtIndex:toViewController:)]) {
            [_delegate_image evaluateCellDidSelectItemAtIndex:imageView.tag-Show_ImageView_Tag toViewController:showVC];
        }
        
    }
}

- (void)awakeFromNib
{
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
